﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace foriisexpress
{
    public partial class list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string path = this.Request.Params["path"];

            if (path == null)
            {
                Response.Write("{\"code\":-1,\"error\":\"need a param :path.\"}");
                return;
            }
            string sPath = Page.Server.MapPath("/");
            var backone = System.IO.Path.GetDirectoryName(sPath);
            path = System.IO.Path.Combine(backone, path);
            StringBuilder sb = new StringBuilder();
            System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(path);
            if (info.Exists == false)
            {
                sb.AppendLine("{");
                sb.AppendLine("\"code\":-2,");
                sb.AppendLine("\"error\":\"path do not exists\"");

                sb.AppendLine("}");

            }
            else
            {
                sb.AppendLine("{");
                sb.AppendLine("\"code\":0,");
                sb.AppendLine("\"subpath\":[");
                {
                    for (int i = 0; i < info.GetDirectories().Length; i++)
                    {
                        if (i != 0) sb.Append(",");

                        sb.AppendLine("{\"name\":\"" + info.GetDirectories()[i].Name + "\"}");
                    }
                }
                sb.AppendLine("],");
                sb.AppendLine("\"subfile\":[");
                {
                    for (int i = 0; i < info.GetFiles().Length; i++)
                    {
                        if (i != 0) sb.Append(",");

                        var f = info.GetFiles()[i];
                        //sb.AppendLine("{\"name\":\"" + f.Name + "\",\"length\":" + f.Length + "}");
                        sb.AppendLine("\"" + f.Name + "\"");
                    }
                }
                sb.AppendLine("]");
                sb.AppendLine("}");
            }
            this.Response.Write(sb.ToString());
        }
    }
}